package com.ifcar.app.util;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ifcar.app.bean.PhotosetListInfo;
import com.ifcar.app.activity.R;


public class PhotoGridAdapter extends BaseAdapter {
	public static final String FLAG = "picture";
	public static final String SIZE_TYPE = "small";
	private String flag;
	private  SharedPreferences set_prefs;

	private class GridHolder {
        ImageView appImage;  
        TextView appName;  
    }  
	private Context context;
	private List<PhotosetListInfo> list;  
    private LayoutInflater mInflater; 
    private BaseImageDownloader mImageDownloader = null;
    public PhotoGridAdapter(Context c) {  
        super();  
        this.context = c;  
        mImageDownloader = new BaseImageDownloader(this.context, null);
        mImageDownloader.setMode(BaseImageDownloader.Mode.CORRECT);
        mImageDownloader.setThread(Thread.currentThread());
        set_prefs = PreferenceManager.getDefaultSharedPreferences(context);

    }
    
    public PhotoGridAdapter(Context c, String type) {  
        super();  
        this.context = c;  
        mImageDownloader = new BaseImageDownloader(this.context, null);
        mImageDownloader.setMode(BaseImageDownloader.Mode.CORRECT);
        mImageDownloader.setThread(Thread.currentThread());
        flag = type;
        set_prefs = PreferenceManager.getDefaultSharedPreferences(context);
     
    }
    
    public void setList(List<PhotosetListInfo> list) {  
        this.list = list;  
        mInflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  
    }  
    
	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		
		return list.get(index);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	int i=0;
	/**
	 * 图集填充
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GridHolder holder;  
		if (convertView == null) {
			holder = new GridHolder();  
			if(SIZE_TYPE.equals(flag)){
				convertView = mInflater.inflate(R.layout.photoset_item_small, null);
				holder.appImage = (ImageView)convertView.findViewById(R.id.photosetitemimage); 
			}else{
				convertView = mInflater.inflate(R.layout.photoset_item, null);    
				holder.appImage = (ImageView)convertView.findViewById(R.id.photosetitemimage);  
	            holder.appName = (TextView)convertView.findViewById(R.id.photosetitemtext);  
			}
            
            convertView.setTag(holder);  
		}else{
			 holder = (GridHolder) convertView.getTag();   
		}
	
		
		PhotosetListInfo photosetlistinfo = list.get(position);
		boolean downpic = set_prefs.getBoolean("downpic", false);
		if(SIZE_TYPE.equals(flag)){
			if(Tools.isshowimg(downpic, context)){
				mImageDownloader.setBitmapWandH(0, 0, null);
				
				mImageDownloader.downloadPictures(photosetlistinfo.getClientcover(),holder.appImage, 110,110, "Y", 
				        ((BitmapDrawable)context.getResources().getDrawable(R.drawable.picture_default)).getBitmap(), FLAG);
			}
			
		}else{
			
			if("0".equals(photosetlistinfo.getSetid())){
				
				holder.appName.setText("");
				holder.appImage.setImageResource(R.drawable.picture_more);
			}else{
				
				holder.appName.setText(photosetlistinfo.getSetname().length()>12?photosetlistinfo.getSetname().substring(0, 10)+"..":photosetlistinfo.getSetname());
				mImageDownloader.setBitmapWandH(0, 0, null);

				if(Tools.isshowimg(downpic, context)){
					mImageDownloader.downloadPictures(photosetlistinfo.getClientcover(),holder.appImage, 170,170, "Y", null, FLAG);
				}
				
			}
			
			
		}
		
		return convertView;
	}
}
